<?php
namespace app\controller;
use app\BaseController;
use app\model\ClassInfo;
use app\model\PostsComment;
use app\model\PostsHot;
use app\model\PostsInfo;
use app\model\User;
use app\model\UserPostsLike;
use app\Request;
use think\facade\Db;
use app\OssClient;

class postController extends BaseController {

    /**
     * 新增帖子
     * @param Request $request
     * @return mixed
     */
    public function insertPosts(Request $request){
        $uid = $request->user_id;
        //获取前端传过来的标题和内容
        $getData = $request->param(['title','content']);
        $cid = User::where('id','=',$uid)->value('class_id');

        $isSuccess = PostsInfo::create([
            'class_id'      => $cid,
            'user_id'       => $uid,
            'post_title'    => $getData['title'],
            'post_content'  => $getData['content'],
            'insert_time'   => time()
        ]);
        if (empty($isSuccess)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功','');
    }

    /**
     * 新增帖子图片
     * @param Request $request
     * @return mixed
     */
    public function insertPostsImg(Request $request){
        $uid = $request->user_id;
        //获取前端传过来的标题和内容
        $getData = $request->param(['title','content']);
        $cid = User::where('id','=',$uid)->value('class_id');
        $img = $request->file('img');

        $oss = new OssClient();
        $src = $img->getPathname();
        $fileName = md5(time()) . md5($uid).".png";
        $folder = 'post';
        $oss->upload($src,$fileName,$folder);
        $path = env('askey.url').$folder.'/'.$fileName;

        $isSuccess = PostsInfo::create([
            'class_id'      => $cid,
            'user_id'       => $uid,
            'post_title'    => $getData['title'],
            'post_content'  => $getData['content'],
            'post_img'  => $path,
            'insert_time'   => time()
        ]);
        if (empty($isSuccess)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功','');
    }

    /**
     * 查询论坛列表
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserPosts(Request $request){
        $posts_info = Db::table('posts_info')->alias('info')
            ->join(['posts_hot'=>'hot'],'info.id=hot.post_id')
            ->field('info.post_img,info.post_title,info.post_content,info.insert_time,info.number_comments,hot.hot')
            ->select();
        if (empty($posts_info)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功',$posts_info);
    }

    /**
     * 首页论坛详情
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetailsPosts(Request $request){
        $pid = $request->get('post_id');
        $details = Db::table('posts_info')->alias('info')
            ->join(['posts_hot'=>'hot'],'info.id=hot.post_id')
            ->field('info.post_title,info.post_img,info.post_content,info.insert_time,hot.hot,info.number_comments,info.number_collections')
            ->find($pid);
        if (empty($details)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功',$details);
    }

    /**
     * 详情页评论查询
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getComments(Request $request){
        $pid = $request->get('post_id');
        $comment = Db::table('user')
            ->join(['posts_comment'=>'comment'],'user.id=comment.user_id')
            ->where('post_id','=',$pid)
            ->field('user.head_img,user.name,comment.like,comment.content,comment.post_time')
            ->select();
        if (empty($comment)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功',$comment);
    }

    /**
     * 详情页评论量查询
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNumberComments(Request $request){
        $pid = $request->get('pid');
        $comment = PostsComment::where('post_id','=',$pid)->count();
        return $this->returnJson(self::SUCCESS,'成功',$comment);
    }

    /**
     * 帖子点赞
     * @param Request $request
     * @return mixed
     */
    public function updateLikePosts(Request $request){
        $cid = $request->get('cid');
        $like = PostsComment::where('id','=',$cid)->value('like');
        $isUpdate = PostsComment::where('id', '=', $cid)->update([
            'like' => $like + 1
        ]);
        if (empty($isUpdate)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功','');
    }

    /**
     * 帖子取消点赞
     * @param Request $request
     * @return mixed
     */
    public function updateNotlikePosts(Request $request){
        $cid = $request->get('cid');
        $like = PostsComment::where('id','=',$cid)->value('like');
        $isUpdate = PostsComment::where('id', '=', $cid)->update([
            'like' => $like - 1
        ]);
        if (empty($isUpdate)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功','');
    }

    /**
     * 详情页评论新增
     * @param Request $request
     * @return mixed
     */
    public function insertComments(Request $request){
        $uid = $request->user_id;
        $getData = $request->param(['pid','content']);
        $commentsNum = PostsInfo::where('id','=',$getData['pid'])->value('number_comments');
        $comment = PostsComment::create([
            'user_id'       => $uid,
            'post_id'       => $getData['pid'],
            'content'       => $getData['content'],
            'post_time'     => time()
        ]);
        $newNum = PostsInfo::where('id', '=', $getData['pid'])->update([
            'number_comments' => $commentsNum + 1
        ]);
        if(empty($comment) && empty($newNum)){
            return $this->returnJson(self::SUCCESS,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'上传成功','');
    }

    /**
     * 帖子收藏
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateCollectionPosts(Request $request){
        $uid = $request->user_id;
        $pid = $request->get('pid');
        $isLike = UserPostsLike::where('user_id','=',$uid)->where('post_id','=',$pid)->find();
        if (!isset($isLike)){
            UserPostsLike::create([
                'user_id'       => $uid,
                'post_id'       => $pid,
                'is_collection' => 1
            ]);
            return $this->returnJson(self::SUCCESS,'收藏成功','');
        }
        $isCollection = UserPostsLike::where('user_id','=',$uid)->where('post_id','=',$pid)->value('is_collection');
        if($isCollection == 1){
            return $this->returnJson(self::USER_ERROR,'你已收藏','');
        }
        $isUpdate = UserPostsLike::where('user_id','=',$uid)->where('post_id','=',$pid)->update([
            'is_collection' => 1
        ]);
        if($isUpdate != 1){
            return $this->returnJson(self::USER_ERROR,'收藏失败','');
        }
        return $this->returnJson(self::SUCCESS,'收藏成功','');
    }


    /**
     * 帖子搜索
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function searchPosts(Request $request){
        $getData = $request->param(['title']);
        $data = PostsInfo::where('post_title','like','%'.$getData['title'].'%')->select();
        if (empty($data)){
            return $this->returnJson(self::SERVER_ERROR,'上传失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功',$data);
    }
}